package com.kodin.polyfit;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.kodin.webview.R;

public class PointFitView extends View {
    private Paint pointPaintSrc, pointPaintTarget, linePaint, paintText;
    private int width, height, padding;
    private int mainBgColor;
    private float[] pointFSrc, pointFTar;

    public PointFitView(Context context) {
        this(context, null);
    }

    public PointFitView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        pointPaintSrc = new Paint();
        pointPaintSrc.setColor(Color.WHITE);
        pointPaintSrc.setStyle(Paint.Style.FILL); // 设置点的填充样式
        pointPaintSrc.setAntiAlias(true);
        pointPaintSrc.setStrokeWidth(1);
        //
        pointPaintTarget = new Paint();
        pointPaintTarget.setColor(Color.YELLOW);
        pointPaintTarget.setStyle(Paint.Style.FILL); // 设置点的填充样式
        pointPaintTarget.setAntiAlias(true);
        pointPaintTarget.setStrokeWidth(5);
        //
        linePaint = new Paint();
        linePaint.setColor(Color.GREEN);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setAntiAlias(true);
        linePaint.setStrokeWidth(1);
        //
        paintText = new Paint();
        paintText.setTextAlign(Paint.Align.CENTER);
        paintText.setColor(Color.RED);
        paintText.setTextSize(ConvertUtils.sp2px(14));//px
        paintText.setStyle(Paint.Style.FILL);

        mainBgColor = context.getResources().getColor(R.color.kodinGray);
    }

    public void setData(double[][] src, double[][] toArray) {
        if (src == null || toArray == null) return;
        pointFSrc = packageSrc(src);
        pointFTar = packageSrc(toArray);
        postInvalidate();
    }

    private float[] packageSrc(double[][] src) {
        int len = src.length;
        float[] srcX = new float[src.length];
        float[] srcY = new float[src.length];
        for (int a = 0; a < len; a++) {
            srcX[a] = (float) src[a][0];
            srcY[a] = (float) src[a][1];
        }
        float stepX = (width - 2 * padding) / findMaxValue(srcX);
        float stepY = (height - 2 * padding) / findMaxValue(srcY);

        float[] pointF = new float[len * 2];
        for (int index = 0; index < len; index++) {
            pointF[index * 2] = srcX[index] * stepX + padding;
            pointF[index * 2 + 1] = height - srcY[index] * stepY - padding;
        }
        return pointF;
    }

    private static float findMaxValue(float[] arr) {
        if (arr == null || arr.length == 0) {
            throw new IllegalArgumentException("输入的数组不能为空");
        }
        float maxVal = Float.MIN_VALUE; // 初始化最大值为Integer类型的最小值
        for (float num : arr) {
            if (num > maxVal) {
                maxVal = num;
            }
        }
        return maxVal;
    }

    private static float findMinValue(float[] arr) {
        if (arr == null || arr.length == 0) {
            throw new IllegalArgumentException("输入的数组不能为空");
        }
        float minVal = Float.MAX_VALUE; // 初始化最大值为Integer类型的最大值
        for (float num : arr) {
            if (num < minVal) {
                minVal = num;
            }
        }
        return minVal;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制背景
        canvas.drawColor(mainBgColor);
        String str = "白色原始 黄色拟合";
        PointF pos = new PointF(width / 2, height / 2);
        onDrawTextCenterCenter(canvas, paintText, pos, str);
        canvas.drawLine(padding, height - padding, padding, padding, linePaint);
        canvas.drawLine(padding, height - padding, width - padding, height - padding, linePaint);
        if (pointFSrc != null) {
            canvas.drawPoints(pointFSrc, pointPaintSrc);
        }
        if (pointFTar != null) {
            canvas.drawPoints(pointFTar, pointPaintTarget);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        padding = width / 100;
    }

    private void onDrawTextCenterCenter(Canvas aCanvas, Paint aPaint, PointF pos, String str) {
        Rect strRect = new Rect();
        aPaint.getTextBounds(str, 0, str.length(), strRect);
        strRect.left += pos.x;
        strRect.top += pos.y;
        strRect.right += pos.x;
        strRect.bottom += pos.y;
        aCanvas.drawText(str, pos.x, pos.y - strRect.height() / 2, aPaint);
    }
}
